#include "auto_boot.h"

enum
{
    rec_head_0,
    rec_head_1,
    rec_head_2,
    rec_head_3,
    rec_head_4,
    rec_done
};

static const char download_cmd_head[] = {0x57, 0xab, 0xa1, 0x12, 0x00};
static uint8_t rec_sta = rec_head_0;
static uint8_t re_flg = 0;

void auto_boot_program_handler()
{
    if (re_flg)
    {
        restart_sys(restart_sys_iap);
    }
}

void auto_boot_program_chek_state(char rec_uart_byte)
{
    if (rec_uart_byte == download_cmd_head[rec_sta])
        rec_sta++;
    else
        rec_sta = rec_head_0;
    if (rec_sta == rec_done)
        re_flg = 1;
}

void restart_sys(sys_restart_t mode)
{
    if (mode == restart_sys_iap)
    {
        void (*boot_f)() = (void *)0;
        boot_f();
    }
    R8_SAFE_ACCESS_SIG = SAFE_ACCESS_SIG1;
    R8_SAFE_ACCESS_SIG = SAFE_ACCESS_SIG2;
    R8_RST_WDOG_CTRL |= RB_SOFTWARE_RESET;
}
